﻿class Viewer {
    public Viewer(vname:String = null) {
        super();

        viewName = vname;
    }

    public function setName(aviewName:String):void {
        viewName = aviewName
    }

    public function getName():String {
        return viewName;
    }

    // called by controller, when c.addViewer is called
    public function setController(c:Controller):void {
        controller = c;
    }

    private function getController():Controller {
        return controller;
    }

    // for one viewer one controller
    public function initController(nc:NetConnection, channelName:String):void {
        var acontroller:Controller = new Controller();
        acontroller.addView(this);
        acontroller.initNC(nc, channelName);
    }

    private notifyServerDoSth(sth) {
        controller.notifyServerDoSth(sth);
    }

    // ---viewer---
    public doSth(sth) {
        // viewer execute sth
    }

    private var viewName:String;
    private var controller:Controller;
}

class Controller {
    private viewers:Array = new Array();
    private so:SOHandler = null;

    public function addViewer(v:Viewer):void {
        v.setController(this);
        viewers.push(v);
    }

    public function initNC(nc:NetConnection, channelName:String) {
        so = new SOHandler(nc, channelName);
        so.setStatusListener(statusChange);
        so.connect();
    }

    public notifyServerDoSth(sth) {
        // sync, server do sth
        so.setP(key, sth);
    }

    function statusChange(asoHandler:SOHandler, pname:String, oldValue:*, newValue:*):void {
        if(pname == null) {
            return;
        }

        newValue = asoHandler.getProperty(pname);

        if(pname == "setData") {
            //processAChange(newValue)
            doSth(newValue);
        }
    }

    private doSth(sth) {
        // v in viewers
        var idx:String = "";

		for(idx in viewers) {
			viewers[parseInt(idx)].doSth(sth);
		}
    }
}


class Controller2 {
    private viewers:Array = new Array();
    private inSO:SOHandler = null;
    private outSO:SOHandler = null;

    public function addViewer(v:Viewer):void {
        v.setController(this);
        viewers.push(v);
    }

    public function initNC(nc:NetConnection, channelName:String) {
        inSO = new SOHandler(nc, channelName);
        inSO.setStatusListener(statusChange);
        inSO.connect();

        outSO = new SOHandler(nc, channelName + "OUT");
        outSO.setStatusListener(statusChange);
        outSO.connect();
    }

    public notifyServerDoSth(sth) {
        // sync, server do sth
        inSO.setP(key, sth);
    }


    private function statusChange(asoHandler:SOHandler, pname:String, oldValue:*, newValue:*):void {
        if(pname == null) {
            return;
        }

        newValue = asoHandler.getProperty(pname);

        if(pname == "setData") {
            //processAChange(newValue)
            doSth(newValue);
        }
    }

    private doSth(sth) {
        // v in viewers
        var idx:String = "";

		for(idx in viewers) {
			viewers[parseInt(idx)].doSth(sth);
		}
    }
}


var viewerName:String = "viewerName";
var channelName:String = "channelName";
var nc:NCHandler = null;


var confID:String;
var userID:String

var viewer:Viewer = new Viewer(viewerName);
// for one viewer one controller
viewer.initController(nc.getNC(), channelName);


----or---

var viewer:Viewer = new Viewer(viewerName);
var acontroller:TController = new TController();
acontroller.initNC(nc, channelName);
acontroller.setViewer(viewer);





